package com.xszx.serviceedu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xszx.commonutils.R;
import com.xszx.servicebase.exceptionhandler.QinXueException;
import com.xszx.serviceedu.client.VodClient;
import com.xszx.serviceedu.entity.Video;
import com.xszx.serviceedu.entity.form.VideoInfoForm;
import com.xszx.serviceedu.mapper.VideoMapper;
import com.xszx.serviceedu.service.VideoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author 白熊
 * @since 2024-07-17
 */
@Service
public class VideoServiceImpl extends ServiceImpl<VideoMapper, Video> implements VideoService {

    @Autowired
    private VodClient vodClient;

    @Override//节目录信息的添加
    public void saveVideoInfo(VideoInfoForm videoInfoForm) {
        //将前端节目录信息赋值给新的节目录
        Video video = new Video();
        BeanUtils.copyProperties(videoInfoForm, video);
        boolean save = this.save(video);
        if (!save) {
            throw new QinXueException(20001, "添加节目录失败");
        }
    }

    @Override//修改节目录信息
    public void updateVideoInfoById(VideoInfoForm videoInfoForm) {
        Video video = new Video();
        BeanUtils.copyProperties(videoInfoForm, video);
        boolean b = this.updateById(video);
        if (!b) {
            throw new QinXueException(20001, "节目录信息修改失败");
        }

    }

    @Override//节目录回显---> 根据id获取节目录
    public VideoInfoForm getVideoInfoFormById(String id) {
        //从video表中取数据
        Video video = this.getById(id);
        if (video == null) {
            throw new QinXueException(20001, "修改节目录未获取到节目录信息");
        }
        //创建videoInfoForm对象
        VideoInfoForm videoInfoForm = new VideoInfoForm();
        BeanUtils.copyProperties(video, videoInfoForm);

        return videoInfoForm;
    }

    @Override//节目录删除
    public boolean removeVideoById(String id) {
        //先通过id查询到该小结
        Video video = baseMapper.selectById(id);
        //根据节目录查询下面的视频资源
        String videoSourceId = video.getVideoSourceId();
        if (!StringUtils.isEmpty(videoSourceId)) {//如果有视频资源就删除
            R r = vodClient.removeVideo(videoSourceId);
//            System.out.println(r+"===========================");
        }

        Integer result = baseMapper.deleteById(id);
        if (result == 0) {
            throw new QinXueException(20001, "节目录删除失败");
        }
        return null != result && result > 0;
    }

    @Override//通过课程id删除节目录
    public Boolean removeByCourseId(String courseId) {
        //使用MP的动态查询语句
        QueryWrapper<Video> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id", courseId);//将id与课程号相对应
        //查到所有的小结
        List<Video> videoList = baseMapper.selectList(wrapper);
        List<String> voidSourseList = new ArrayList<>();

        for (Video video : videoList) {//将要删除的小结放进去
            if (StringUtils.isNotEmpty(video.getVideoSourceId())) {
                voidSourseList.add(video.getVideoSourceId());
            }
        }
        //调用批量删除方法
        vodClient.removeVideoList(voidSourseList);

        //假如出现本身就没有节目录的课程判断
        Integer count = baseMapper.selectCount(wrapper);
        if (count != 0) {//有条数的话删除 没有的话就不用删
            //删除节目录
            Integer i = baseMapper.delete(wrapper);
            if (i == 0) {
                throw new QinXueException(20001, "删除节目录失败");
            }
        }
        return null != count && count > 0;

    }

}
